package com.zkx.mvp.view.Fragment;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;

import com.zkx.mvp.R;
import com.zkx.mvp.adapter.chat.MsgListAdapter;
import com.zkx.mvp.interfaces.ItemClickListener;
import com.zkx.mvp.itemtouch.MyItemSlideTouchHelper;
import com.zkx.mvp.itemtouch.MyItemTouchHelperCallback;
import com.zkx.mvp.model.chat.MsgItem;
import com.zkx.mvp.utils.ToastUtil;
import com.zkx.mvp.view.BaseFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.zkx.mvp.R.id.recyclerView;


/**
 *
 */
public class ChatFragment extends BaseFragment {
    @BindView(recyclerView)
    RecyclerView mRecyclerView;
    Unbinder unbinder;

    private List<MsgItem> mList;//测试数据集合
    // 图片集合
    private int images[] = new int[]{
            R.mipmap.android,
            R.mipmap.batman,
            R.mipmap.deadpool,
            R.mipmap.gambit,
            R.mipmap.hulk,
            R.mipmap.mario,
            R.mipmap.wolverine,
            R.mipmap.daredevil
    };
    // 标题集合
    private String names[] = new String[]{
            "Android",
            "Batman",
            "DeadPool",
            "Gambit",
            "Hulk",
            "Mario",
            "Wolverine",
            "Daredevil"
    };
    // 标题集合
    private String description[] = new String[]{
            "Android,时下最火",
            "Batman,拯救世界的男人",
            "DeadPool,拯救batMan的男人",
            "Gambit，破坏世界的男人",
            "Hulk,他就是绿巨人浩克，打恐龙都不在话下",
            "Mario,还记得儿时的好朋友吗",
            "Wolverine，狼叔，铁汉柔情",
            "Daredevil，，在座的各位都是垃圾"
    };
    private MsgListAdapter mAdapter;
    /**
     * 滑动拖拽的帮助类
     */
    private MyItemSlideTouchHelper itemTouchHelper;

    @Override
    protected View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_test, null);
        unbinder = ButterKnife.bind(this, view);
        setContentShown(true);
        return view;
    }

    @Override
    protected void initData() {
        mLoadingDialog.show();
        //初始化测试数据
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                cancleLoading();
                initList();
            }
        }, 2000);

    }

    //设置测试数据
    private void initList() {
        mList = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            boolean isCheck = i % 2 == 0;
            mList.add(new MsgItem(images[i], names[i], description[i], isCheck));
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mAdapter = new MsgListAdapter(context, mList);
        mRecyclerView.setAdapter(mAdapter);
        // 把ItemTouchHelper和itemTouchHelper绑定
        itemTouchHelper = new MyItemSlideTouchHelper(onItemTouchCallbackListener);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
        mAdapter.setItemTouchHelper(itemTouchHelper);

        itemTouchHelper.setDragEnable(true);
        itemTouchHelper.setSwipeEnable(true);
        /**
         * item 点击监听
         */
        mAdapter.setItemClick(new ItemClickListener<MsgItem>() {
            @Override
            public void onItemClick(View view, MsgItem data) {
                ToastUtil.showToast(context, "您选择的是" + data.getName());

            }
        });
        /**
         * item 选中反选监听
         */
        mAdapter.setOnCheckedChangeListener(new MsgListAdapter.OnCheckedChangeListener() {
            @Override
            public void onItemCheckedChange(CompoundButton view, int position, boolean checked) {
                ToastUtil.showToast(context, "第" + position + (checked ? "选中" : "被反选") +mList.get(position).getName());
            }
        });

    }

    /**
     * 取消加载框
     */
    private void cancleLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private MyItemTouchHelperCallback.OnItemTouchCallbackListener onItemTouchCallbackListener = new MyItemTouchHelperCallback.OnItemTouchCallbackListener() {
        @Override
        public void onSwiped(int adapterPosition) {
            if (mList != null) {
                mList.remove(adapterPosition);
                mAdapter.notifyItemRemoved(adapterPosition);
            }
        }

        @Override
        public boolean onMove(int srcPosition, int targetPosition) {
            if (mList != null) {
                // 更换数据源中的数据Item的位置
                Collections.swap(mList, srcPosition, targetPosition);

                // 更新UI中的Item的位置，主要是给用户看到交互效果
                mAdapter.notifyItemMoved(srcPosition, targetPosition);
                return true;
            }
            return false;
        }
    };

}
